from django.db import models
from django.contrib.auth.models import User
from django.contrib.localflavor.us.us_states import STATE_CHOICES
from django.contrib.localflavor.us.models import USStateField

    
class Title(models.Model):
    title = models.CharField(max_length=20)
    def __unicode__(self):
        return self.title
    
class UserProfile(models.Model):
    user = models.OneToOneField(User)
    id = models.AutoField(primary_key=True)
    address1 = models.CharField(max_length=30, verbose_name='Address Line 1', blank=True,  null=True)
    address2 = models.CharField(max_length=30, verbose_name='Address Line 2', blank=True,  null=True)
    city = models.CharField(max_length=20, verbose_name='City', blank=True,  null=True)
    state = USStateField(choices=STATE_CHOICES)
    postal_code = models.CharField(max_length=5, verbose_name='Zip Code', blank=True,  null=True)
 
    home_phone = models.CharField(max_length=20, verbose_name='Home Phone', blank=True,   null=True)
    mobile_phone = models.CharField(max_length=20, verbose_name='Cell Phone', blank=True,   null=True)
    dob = models.DateField(null=True, blank=True)
    # Gender
    MALE = 'M'
    FEMALE = 'F'
    OTHER = 'O'
    GENDER_CHOICES = (
        (MALE, 'Male'),
        (FEMALE, 'Female'),
        (OTHER, 'Other'),
    )
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default=MALE)    
    def __unicode__(self):
        return self.user.first_name + " " + self.user.last_name + ' (' + self.user.username +')'
    
    
# 
# # Employee Profile with one-to-one relationship to a single employee
class EmployeeProfile(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.OneToOneField(UserProfile, unique=True)
    title = models.ForeignKey(Title)
    hire_date = models.DateField(null=True, verbose_name='Date of Hire')
    ACTIVE = "A"
    INACTIVE = "I"
    EMPLOYEE_STATUSES = (
        (ACTIVE, 'Current Employee'),
        (INACTIVE, 'Former Employee'),
    )
    employee_status = models.CharField(max_length=1,choices=EMPLOYEE_STATUSES, default=ACTIVE)
    class Meta:
        verbose_name_plural = "Employee Profiles"
        #ordering = [user.last_name, employee_status]
    def __unicode__(self):
        return  self.user.user.last_name + ", " + self.user.user.first_name + " (" + self.user.user.username + ") "

class Doctor(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.OneToOneField(UserProfile)
    class Meta:
        verbose_name_plural = "Doctors"  

# 
# # Patient Profile with one-to-one relationship to a single user
class PatientProfile(models.Model):
    user = models.OneToOneField(UserProfile, unique=True)
    id = models.AutoField(primary_key=True)
    height = models.CharField(max_length=30, verbose_name='Height', blank=True, null=True)
    weight = models.CharField(max_length=30, verbose_name='Weight', blank=True, null=True)
    last_visit = models.DateField(verbose_name='Date of last visit', blank=True, null=True)
    
    # Gender
    ACTIVE = 'A'
    INACTIVE = 'I'
    OTHER = 'O'
    PATIENT_STATUSES = (
        (ACTIVE, 'Current Patient'),
        (INACTIVE, 'Former Patient'),
        (OTHER, 'Other'),
    )
    patient_status = models.CharField(max_length=1, choices=PATIENT_STATUSES, default=ACTIVE)
    primary_doctor = models.ForeignKey(Doctor, blank=True, null=True)
    class Meta:
        verbose_name_plural = "Patient Profiles"
        ordering = ["user"]
    def __unicode__(self):
        return  self.user.user.first_name + " " + self.user.user.last_name + " (" + self.user.user.username + ")"


class Medication(models.Model):
    id = models.AutoField(primary_key=True)
    common_name = models.CharField(max_length = 50)
    name = models.CharField(max_length = 50, blank=True, null=True)
    patient = models.ManyToManyField("PatientProfile", through="Prescription")
    ACTIVE = 1
    INACTIVE = 2
    class Meta:
        verbose_name_plural = "Medications"
    def __unicode__(self):
        return  self.common_name

class Diagnosis(models.Model):
    id = models.AutoField(primary_key=True)
    patient = models.ForeignKey("PatientProfile")
    description = models.CharField(max_length=50, blank=True, null=True)
    class Meta:
        verbose_name_plural = "Diagnoses"
    def __unicode__(self):
        return  self.description
    

class Prescription(models.Model):
    id = models.AutoField(primary_key=True)
    patient = models.ForeignKey("PatientProfile")
    medication = models.ForeignKey("Medication")
    refills = models.IntegerField();
    prescribed_date = models.DateField(blank=True,  null=True, verbose_name='Date Prescribed')
    expiration_date = models.DateField(blank=True,  null=True, verbose_name='Expiration Date')
    ACTIVE = "A"
    INACTIVE = "I"
    PRESCRIPTION_STATUSES = (
        (ACTIVE, 'Active'),
        (INACTIVE, 'Expired'),
    )
    prescription_status = models.CharField(max_length=1,choices=PRESCRIPTION_STATUSES, default=ACTIVE)
    class Meta:
        verbose_name_plural = "Patient Prescriptions"
    def __unicode__(self):
        return  self.medication.common_name
    
class PatientAppointment(models.Model):
    id = models.AutoField(primary_key=True)
    patient = models.ForeignKey("PatientProfile")
    appointment_date = models.DateTimeField(blank=True, null=True, verbose_name='Date and Time of Appointment')
    reason_for_appointment = models.TextField(blank=True, null=True, verbose_name="Reason for Visit")
    appointment_notes = models.TextField(blank=True, null=True, verbose_name="Notes")
    class Meta:
        verbose_name = "Patient Appointment"
    def __unicode__(self):
        return  self.patient.user.user.first_name + " " + self.patient.user.user.last_name 
  
class Staff(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.OneToOneField(UserProfile)
    class Meta:
        verbose_name_plural = "Staff Members"
  
class Administrator(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.OneToOneField(UserProfile)
    class Meta:
        verbose_name = "Administrator"
        verbose_name_plural = "Administrators"

# definition of UserProfile
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)
